Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
Reaccionar cambiar solo el texto del botón pulsado [En bucle]

Cuando hago clic en el botón, ejecuto una función llamada AddWait pero agrega texto de espera a todos los botones. Probé con useRef pero las cosas se complicaron. Imagen;

botón-no-cambiar

Hice algo similar a esto con jquery;

 for (let index = 0; index <= 10; index++) { $("#app").append(`<span>Hello ${index} </span> ===&gt;<button class=click-me>button ${index} </button> <br /><br />`); } $(".click-me").on("click", function (event) { var $this = $(this); console.log($this.text()); $this.text($this.text() + "hi"); //(... rest of your JS code) });
 <div id="app"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Puede verificar la aplicación de reacción;

Entonces, ¿cómo puedo cambiar solo el texto del botón en el que se hizo clic?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

import { useState } from "react"; import "./styles.css"; export default function App() { const [waitText, setWaitText] = useState(); const [arr,setArr] = useState([ { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, { waitText:"" }, ]); const WaitThreeSeconds = (index) => { console.log("hi"); let temp = [...arr]; temp[index].waitText = "wait" setArr([...temp]) // setWaitText("wait"); }; return ( <div className="App"> {arr.map((x, i) => ( <div key={i}> <span>Hello {i} </span> <button onClick={()=>WaitThreeSeconds(i)}> button {i} {x.waitText} </button> <br /> <br /> </div> ))} </div> ); }

Puede consultar el código de la caja de arena aquí . Espero que le sea útil :)

about 4 years ago · Juan Pablo Isaza Report

0

Lo que debe hacer es guardar el div con el botón en un componente separado y luego reaccionar cambiará el texto del botón solo para ese componente en particular:

 import { useState } from "react"; import "./styles.css"; const CustomBtn = props => { const { item, index } = props; console.log(item) const [waitText, setWaitText] = useState(''); const waitThreeSeconds = () => { setWaitText("wait"); setTimeout(() => setWaitText(''), 3000) }; return ( <div> <span>Hello {index} </span> <button onClick={waitThreeSeconds}> button {index} {waitText} </button> <br /> <br /> </div> ) } export default function App() { return ( <div className="App"> {[...Array(10)].map((item, index) => ( <CustomBtn item={item} key={index} index={index} /> ))} </div> ); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!